package com.example.jxl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class jxlTest {

	public static void main(String[] args) throws IOException {
		File file0 = new File("C:/Users/Administrator/Desktop/麦客报名表.xls");
		File file1 = new File("C:/Users/Administrator/Desktop/百度手机助手名单.xls");
		File file2 = new File("C:/Users/Administrator/Desktop/百度手机助手名单2.xls");

		Map<Integer, Integer> map = new HashMap<Integer, Integer>();

		try {
			Workbook book = Workbook.getWorkbook(file0);
			Sheet sheet = book.getSheet(0);

			int rows = sheet.getRows();

			for (int i = 2; i < rows - 4; i++) {
				try {
					Cell cell = sheet.getCell(3, i);

					Integer uid = Integer.valueOf(cell.getContents());

					map.put(uid, uid);

					// System.out.println(cell.getContents());
				} catch (Exception e) {}
			}

			System.out.println("麦客报名表.xls 读取完");

			book.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			Workbook book = Workbook.getWorkbook(file1);
			WritableWorkbook wbook = Workbook.createWorkbook(file2);

			Sheet sheet = book.getSheet(0);
			WritableSheet wsheet = wbook.createSheet("sheet0", 0);
			WritableSheet wsheet_repeat = wbook.createSheet("重复", 1);

			int rows = sheet.getRows();

			for (int i = 1, j = 1, k = 0; i < rows; i++) {
				Cell cell = sheet.getCell(0, i);
				Cell cell2 = sheet.getCell(1, i);

				Integer uid = Integer.valueOf(cell.getContents());

				if (map.containsKey(uid)) {
					k++;

					wsheet_repeat.addCell(new Label(0, k, String.valueOf(uid)));
					wsheet_repeat.addCell(new Label(1, k, cell2.getContents()));
				} else {
					j++;

					wsheet.addCell(new Label(0, j, String.valueOf(uid)));
					wsheet.addCell(new Label(1, j, cell2.getContents()));
				}
			}

			System.out.println("百度手机助手名单.xlsx 读取完");

			wbook.write();// 写入Excel对象
			wbook.close();// 关闭可写入的Excel对象

			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
